/*
 * Copyright 2009 Lukasz Wozniak
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package pl.lwozniak.blip.message;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import pl.lwozniak.http.HttpStrings;

/**
 * Message part for files. 
 * No MIME magic, sry ;) if no content type supplied
 * defaults to <code>application/octet-stream</code>
 * 
 * @author Lukasz Wozniak
 * 
 */
public class FileMessagePart extends BlipMessagePart {

	private InputStream stream;
	private byte[] bytes;
	private String filename;
	private long fileSize;

	public FileMessagePart(String contentType, String name, String fileName, InputStream stream, long fileSize) {
		super(contentType,name);
		this.stream = stream;
		this.filename = fileName;
		this.fileSize = fileSize;
	}

	public FileMessagePart(String contentType, String name, String fileName, byte[] bytes) {
		super(contentType,name);
		this.bytes = bytes;
		this.filename = fileName;
		this.fileSize = bytes.length;
	}

	public FileMessagePart(String name, String fileName,InputStream stream) {
		super(HttpStrings.APPLICATION_OCTET_STREAM,name);
		this.stream = stream;
		this.filename = fileName;
	}

	public FileMessagePart(String name, String fileName,byte[] bytes) {
		super(HttpStrings.APPLICATION_OCTET_STREAM,name);
		this.bytes = bytes;
		this.filename = fileName;
	}

	public String getFilename() {
		return filename;
	}

	/**
	 * Provides input stream from supplied content.
	 * 
	 * @return
	 */
	public InputStream getStream() {
		if (stream == null) {
			return new ByteArrayInputStream(bytes);
		}
		return stream;
	}

	public long getFileSize() {
		return fileSize;
	}

}
